<?php

namespace App\Http\Controllers\Common;

use App\Model\Comment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
    public function __construct()
    {
//        $this->middleware('guest',['only'=>['index']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $model=hd_model();
//        dd($model);
        $comments=$model->comment()->with(['user'])->get();
//        dd($comments);
        return ['comments'=>$comments,'code'=>0];
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

      $model= hd_model();
//      dd($model->toArray());
       //return $request->all();
//        return $model;
        $data=$request->only(['content']);
        $data['user_id']=auth()->id();
        //dd($data);
//        return $data;die;
       $comment= $model->comment()->create($data);
        //返回评论内容到页面
//        $comment['user']=$comment->user;
//        return ['comment'=>$comment,'code'=>0];
        //或者一下面这种方式返回数据\
        return ['comments'=>$comment->with(['user'])->find($comment['id']),'code'=>0];

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function show(Comment $comment)
    {
        //消息已读后标记为已读


    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function edit(Comment $comment)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Comment $comment)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function destroy(Comment $comment)
    {
        //
    }
}
